package cmpe451.group7.ourcity.persistance;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;

import cmpe451.group7.ourcity.dao.UserDAO;
import cmpe451.group7.ourcity.dao.impl.UserDAOImpl;
import cmpe451.group7.ourcity.entity.User;
import cmpe451.group7.ourcity.util.PasswordHash;

/**
 * Class to fill the HSQLDB
 * 
 * @author gokhan
 * 
 */
public class DatabaseInitializer {
	public void addUsers() {
		UserDAO userDAO = new UserDAOImpl();
		User user = null;
		Scanner scanner = new Scanner(DatabaseInitializer.class
				.getClassLoader().getResourceAsStream("db/users.txt"));
		int noOfUsers = scanner.nextInt();
		scanner.nextLine();

		while (noOfUsers-- > 0) {
			String fields[] = scanner.nextLine().split(",");
			user = new User();

			user.setName(fields[0]);
			user.setSurname(fields[1]);
			user.setEmail(fields[2]);
			try {
				user.setPassword(PasswordHash.createHash(fields[3]));
			} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
				e.printStackTrace();
			}

			userDAO.save(user);
		}

		scanner.close();
	}

	public void addPlaces() {

	}

	public void initialize() {
		addUsers();
	}
}
